﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using MiniErp.Repository.IRepository;
using MiniErp.Repository.Repository;
using MiniErp.DBService.Service.IService;
using MiniErp.DBService.Service.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MiniErp.Mysql.Data;
using Microsoft.EntityFrameworkCore;
using MiniErp.Model.Templates;
using MiniErp.Model.Entities;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using MiniErp.Repository.IRepository.IAssetManagement;
using MiniErp.Repository.Repository.AssetManagement;
using MiniErp.Service.DBService.Service.AssetManagement;
using MiniErp.Service.DBService.IService.IAssetManagement;

namespace MiniErp.App
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        /// <summary>
        /// 管理组件依赖（依赖注入服务:对比在需要使用服务的地方创建实例的优点（有生命周期管控，并且低耦合：更换服务类后只需要在此修改就行，不会影响控制层））
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {

            //注册api
            services.AddControllers();

            #region DB业务
            //注册mysql context
            services.AddDbContext<MysqlContext>(options =>
               options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));

            services.AddTransient<IUsetInfoRepository, UsetInfoRepository>();
            services.AddTransient<ILoginService, LoginService>();
            #region 资产管理系统
            services.AddTransient<IAssetInfoRepository, AssetInfoRepository>();
            services.AddTransient<IAssetIndexService, AssetIndexService>();
            #endregion
            #endregion

            //注册Swagger
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "MiniErp.App",
                });
            });

        }

        /// <summary>
        /// 配置HTTP请求通道（加载中间件）
        /// 1.通过IApplicationBuilder创建请求通道
        /// 2.每个中间件都可以截获，修改，传递请求对象，输出响应对象
        /// 3.某些中间件可以进行短路操作，及直接返回信息给前端，而不是输出响应对象给下一个中间件
        /// </summary>
        /// <param name="app">请求通道</param>
        /// <param name="env">环境</param>
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            //环境判断
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MiniErp.App v1"));
            }
            //  app.UseAuthentication();
            app.UseHttpsRedirection();
            //路由中间件
            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
